package com.orchard.orchardfarmmanagementsystem.controller;


import com.orchard.orchardfarmmanagementsystem.dto.MaterialDTO;
import com.orchard.orchardfarmmanagementsystem.dto.ResetPasswordDTO;
import com.orchard.orchardfarmmanagementsystem.dto.TaskDTO;
import com.orchard.orchardfarmmanagementsystem.dto.UserRoleDTO;
import com.orchard.orchardfarmmanagementsystem.entity.Role;
import com.orchard.orchardfarmmanagementsystem.entity.User;
import com.orchard.orchardfarmmanagementsystem.service.UserService;
import com.orchard.orchardfarmmanagementsystem.utils.Result;
import com.orchard.orchardfarmmanagementsystem.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/users")
    @PreAuthorize("hasAuthority('PERM_READ_DATA')")
    public Result<List<User>> getUserInfo() {
        try {
            List<User> users = userService.getAllUsers();
            return Result.success("获取用户信息成功", users);
        } catch (Exception e) {
            return Result.error(50001, "获取用户信息失败");

        }


    }
    @GetMapping("/users-role")
    @PreAuthorize("hasAuthority('PERM_READ_DATA')")
    public Result<List<UserRoleDTO>> getUserRole() {
        try {
            List<UserRoleDTO> users = userService.getAllUsersAndRole();
            return Result.success("获取用户信息成功", users);
        } catch (Exception e) {
            return Result.error(50001, "获取用户信息失败");

        }


    }

    @GetMapping("/roles")
    @PreAuthorize("hasAuthority('PERM_READ_DATA')")
    public Result<List<Role>> getRoles() {
        try {
            List<Role> roles = userService.getAllRoles();
            return Result.success("获取角色信息成功", roles);
        } catch (Exception e) {
            return Result.error(50001, "获取角色信息失败");

        }
    }

    @PostMapping("/users")
    @PreAuthorize("hasAuthority('PERM_USER_MANAGEMENT')")
    public Result adduser(@RequestBody UserRoleDTO userRoleDTO) {
        try {
            userService.addUser(userRoleDTO);
            return Result.success("新增用户信息成功");
        } catch (Exception e) {
            return Result.error(50001, e.getMessage());

        }
    }

    @PutMapping("users/{id}")
    @PreAuthorize("hasAuthority('PERM_USER_MANAGEMENT')")
    public Result editUser(@PathVariable Integer id, @RequestBody UserRoleDTO UserRoleDTO) {
        try {
            userService.editUser(id, UserRoleDTO);
            return Result.success("编辑用户信息成功");
        } catch (Exception e) {
            return Result.error(50001, "编辑用户信息失败");
        }
    }

    @DeleteMapping("users/{id}")
    @PreAuthorize("hasAuthority('PERM_USER_MANAGEMENT')")
    public Result deleteUser(@PathVariable Integer id) {
        try {
            userService.deleteUser(id);
            return Result.success("删除用户信息成功");
        } catch (Exception e) {
            return Result.error(50001, e.getMessage());
        }
    }

    @PostMapping("users/{id}/reset-password")
    @PreAuthorize("hasAuthority('PERM_USER_MANAGEMENT')")
    public Result resetPassword(@PathVariable Integer id, @RequestBody ResetPasswordDTO resetPasswordDTO) {
        try {
            userService.resetPassword(id, resetPasswordDTO);
            return Result.success("重置用户密码成功");
        } catch (Exception e) {
            return Result.error(50001, e.getMessage());
        }
    }
}